/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package filecopier.gui.restore;

import filecopier.script.ScriptPath;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.event.ItemListener;
import java.awt.event.FocusListener;
/**
 *
 * @author david
 * 
 * This class serves as a bridge between the ScriptPath object for the Restore Path, the Restore tab, and
 * the Miscellaneous tab. The restore tab has a combo box that allows users to change the order of directories
 * in the restore path and the Miscellaneous tab has a text field that allows users to edit the restore path.
 * This class synchonizes the ScriptPath, the combo box in the restore tab, and the text field in the miscellaneous
 * path.
 */
public class RestoreScriptPathWrapper implements FocusListener, ItemListener {
    
    private ScriptPath scriptPath = null;
    private JTextField textField = null;
    private JComboBox comboBox = null;
    private String textFieldValue = null;
    
    public RestoreScriptPathWrapper(final ScriptPath scriptPath) {
        setRestorePath(scriptPath);
    }
    
    public void setRestorePath(final ScriptPath scriptPath) {
        this.scriptPath = scriptPath;
    }
    
    public ScriptPath getRestorePath() {
        return scriptPath;
    }
    
    // This is the text field for the Restore Path in the Miscellaneous Tab.
    public void setJTextField(final JTextField textField) {
        this.textField = textField;
        this.textField.addFocusListener(this);
    }
    
    // This is the combo box in the restore tab.
    public void setJComboBox(final JComboBox comboBox) {
        this.comboBox = comboBox;
        comboBox.addItemListener(this);
    }
    
    public void focusGained(java.awt.event.FocusEvent fe) {
        if(textField != null) {
            textFieldValue = textField.getText();
        }
    }
    
    public void focusLost(java.awt.event.FocusEvent fe) {
        if(textField != null && !textField.getText().equals(textFieldValue)) {
            updatePath(textField.getText());
        }
    }

    private void updatePath(String pathValue) {
        scriptPath.setPath(pathValue);
        if(comboBox != null) {
            String[] pathArray = scriptPath.getPathArray();
            try {
                // Remove the item listener because I will be changing the comboBox's contents below and don't want to 
                // trigger the item listener when I do.
                comboBox.removeItemListener(this);
                comboBox.removeAllItems();
                // Add in opposite order because the combo box in the restore tab uses a UniqueStackComboBoxModel
                // and everything that gets added goes to the front.
                for(int i = pathArray.length-1; i >= 0; i--) {
                    comboBox.addItem(pathArray[i]);
                }
            } finally {
                // Add the item listener back.
                comboBox.addItemListener(this);
            }
        }
    }
    
    // When we get an itemStateChanged event, the user selected an item in the combo box of the restore tab.
    // Whenever user selects an item in this combo box, put it at the beginning of scriptPath.
    public void itemStateChanged(java.awt.event.ItemEvent e) {
        if(e.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
            int idx = scriptPath.getPathEntryIndex((String)e.getItem());
            if(idx > 0) {
                scriptPath.reorderPath(0, idx);
            }
            // Update the JTextField in the Miscellaneous tab.
            if(textField != null) {
                textField.setText(scriptPath.getPath());
            }
        }
    }
}
